<template>
  <div class="forgot-password-page">
    <div class="forgot-password-container">
      <div class="logo">
        <h1>AI智慧备课系统</h1>
        <p>找回你的账户</p>
      </div>
      <form
        class="forgot-password-form"
        @submit.prevent="handleSubmit"
      >
        <div class="form-group">
          <i class="fas fa-envelope" />
          <input
            v-model="form.emailOrPhone"
            type="text"
            placeholder="请输入注册邮箱或手机号"
            required
            @input="clearError('emailOrPhone')"
          >
          <div
            v-if="errors.emailOrPhone"
            class="error-message"
          >
            {{ errors.emailOrPhone }}
          </div>
        </div>

        <!-- 新增新密码输入框 -->
        <div class="form-group">
          <i class="fas fa-lock" />
          <input
            v-model="form.newPassword"
            type="password"
            placeholder="请输入新密码"
            required
            @input="clearError('newPassword')"
          >
          <div
            v-if="errors.newPassword"
            class="error-message"
          >
            {{ errors.newPassword }}
          </div>
        </div>

        <!-- 新增确认新密码输入框 -->
        <div class="form-group">
          <i class="fas fa-lock" />
          <input
            v-model="form.confirmNewPassword"
            type="password"
            placeholder="请确认新密码"
            required
            @input="clearError('confirmNewPassword')"
          >
          <div
            v-if="errors.confirmNewPassword"
            class="error-message"
          >
            {{ errors.confirmNewPassword }}
          </div>
        </div>

        <div
          v-if="showVerificationCode"
          class="form-group"
        >
          <i class="fas fa-shield-alt" />
          <input
            v-model="form.verificationCode"
            type="text"
            placeholder="请输入验证码"
            required
            @input="clearError('verificationCode')"
          >
          <button
            type="button"
            class="get-code-btn"
            :disabled="isSendingCode || countdown > 0"
            @click="sendVerificationCode"
          >
            {{ countdown > 0? `${countdown}秒后重试` : '获取验证码' }}
          </button>
          <div
            v-if="errors.verificationCode"
            class="error-message"
          >
            {{ errors.verificationCode }}
          </div>
        </div>

        <button
          type="submit"
          class="reset-password-btn"
          :disabled="isSubmitting"
        >
          <span v-if="!isSubmitting">重置密码</span>
          <span v-else>处理中</span>
        </button>

        <div class="back-section">
          <button
            type="button"
            class="back-btn"
            @click="goBack"
          >
            <i class="fas fa-arrow-left" /> 返回登录
          </button>
        </div>
      </form>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue';  // 只保留需要的导入
import { useRouter } from 'vue-router';

// 表单数据
const form = ref({
  emailOrPhone: '',
  verificationCode: '',
  newPassword: '',
  confirmNewPassword: ''
});

const isSubmitting = ref(false);
const isSendingCode = ref(false);
const errors = ref({
  emailOrPhone: '',
  verificationCode: '',
  newPassword: '',
  confirmNewPassword: ''
});

const router = useRouter();
const countdown = ref(0);
const showVerificationCode = ref(false);

// 表单验证
const validateForm = () => {
  let isValid = true;

  // 邮箱/手机号验证
  if (!form.value.emailOrPhone.trim()) {
    errors.value.emailOrPhone = '请输入注册邮箱或手机号';
    isValid = false;
  } else if (!/^(1[3-9]\d{9}|[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,})$/.test(form.value.emailOrPhone)) {
    errors.value.emailOrPhone = '请输入正确的邮箱或手机号';
    isValid = false;
  }

  // 新密码验证
  if (!form.value.newPassword) {
    errors.value.newPassword = '请输入新密码';
    isValid = false;
  } else if (form.value.newPassword.length < 6) {
    errors.value.newPassword = '新密码长度至少6位';
    isValid = false;
  }

  // 确认新密码验证
  if (form.value.newPassword!== form.value.confirmNewPassword) {
    errors.value.confirmNewPassword = '两次输入的新密码不一致';
    isValid = false;
  }

  // 验证码验证
  if (showVerificationCode.value &&!form.value.verificationCode.trim()) {
    errors.value.verificationCode = '请输入验证码';
    isValid = false;
  }

  return isValid;
};

// 清除错误信息
const clearError = (field) => {
  errors.value[field] = '';
};

// 发送验证码
const sendVerificationCode = async () => {
  if (!validateForm() || isSendingCode.value || countdown.value > 0) return;

  isSendingCode.value = true;

  try {
    // 模拟API调用
    await sendCode();

    // 开始倒计时
    countdown.value = 60;
    const timer = setInterval(() => {
      countdown.value--;
      if (countdown.value <= 0) {
        clearInterval(timer);
      }
    }, 1000);

    // 显示验证码输入框
    showVerificationCode.value = true;
    alert('验证码已发送，请查收');
  } catch (error) {
    errors.value.emailOrPhone = '发送验证码失败，请重试';
  } finally {
    isSendingCode.value = false;
  }
};

// 模拟发送验证码API
const sendCode = () => {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      // 模拟校验，实际应该替换为真实API调用
      if (form.value.emailOrPhone) {
        resolve();
      } else {
        reject(new Error('发送失败'));
      }
    }, 1000);
  });
};

// 提交表单
const handleSubmit = async () => {
  if (!validateForm()) return;

  isSubmitting.value = true;

  try {
    // 模拟API调用
    await resetPassword();

    alert('密码重置成功！请使用新密码登录');
    router.push('/login');
  } catch (error) {
    errors.value.verificationCode = '验证码错误，请重试';
  } finally {
    isSubmitting.value = false;
  }
};

// 模拟重置密码API
const resetPassword = () => {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      // 模拟校验，实际应该替换为真实API调用
      if (form.value.verificationCode === '123456') {
        resolve();
      } else {
        reject(new Error('重置失败'));
      }
    }, 1000);
  });
};

// 返回登录页
const goBack = () => {
  router.push('/login');
};
</script>

<style scoped>
.forgot-password-page {
  margin: 0;
  height: 100vh;
  width: 100vw;
  display: flex;
  justify-content: center;
  align-items: center;
  background: linear-gradient(rgba(15, 23, 42, 0.8), rgba(15, 23, 42, 0.9)),
  url('https://images.unsplash.com/photo-1451187580459-43490279c0fa?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&w=2072&q=80');
  background-size: cover;
  background-position: center;
}

.forgot-password-container {
  width: 100%;
  max-width: 420px;
  background: rgba(30, 41, 59, 0.8);
  border-radius: 16px;
  box-shadow: 0 10px 25px rgba(99, 102, 241, 0.3);
  padding: 40px 30px;
  backdrop-filter: blur(8px);
  border: 1px solid rgba(99, 102, 241, 0.2);
  animation: fadeIn 0.6s ease-out;
  position: relative;
  overflow: hidden;
}

.forgot-password-container::before {
  content: '';
  position: absolute;
  top: -50%;
  left: -50%;
  width: 200%;
  height: 200%;
  background: radial-gradient(circle, rgba(99, 102, 241, 0.1) 0%, transparent 70%);
  z-index: -1;
}

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(20px); }
  to { opacity: 1; transform: translateY(0); }
}

.logo {
  text-align: center;
  margin-bottom: 30px;
}

.logo h1 {
  color: white;
  font-size: 24px;
  font-weight: 700;
  margin-bottom: 8px;
  background: linear-gradient(90deg, #e2e8f0, #6366f1);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
}

.logo p {
  color: #94a3b8;
  font-size: 14px;
  letter-spacing: 0.5px;
}

.forgot-password-form .form-group {
  position: relative;
  margin-bottom: 25px;
}

.forgot-password-form .form-group i {
  position: absolute;
  left: 15px;
  top: 50%;
  transform: translateY(-50%);
  color: #64748b;
  font-size: 18px;
  transition: all 0.3s;
}

.forgot-password-form input {
  width: 100%;
  padding: 15px 15px 15px 5px;
  background: rgba(15, 23, 42, 0.7);
  border: 2px solid #334155;
  border-radius: 8px;
  font-size: 16px;
  transition: all 0.3s;
  outline: none;
  color: white;
}

.forgot-password-form input::placeholder {
  color: #64748b;
}

.forgot-password-form input:focus {
  border-color: #6366f1;
  box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.3);
}

.forgot-password-form input:focus + i {
  color: #6366f1;
}

.forgot-password-form input.error {
  border-color: #ef4444;
}

.get-code-btn {
  position: absolute;
  right: 10px;
  top: 50%;
  transform: translateY(-50%);
  background: #6366f1;
  color: white;
  border: none;
  border-radius: 6px;
  padding: 8px 12px;
  font-size: 14px;
  cursor: pointer;
  transition: all 0.3s;
}

.get-code-btn:hover {
  background: #4f46e5;
}

.get-code-btn:disabled {
  background: #475569;
  cursor: not-allowed;
}

.reset-password-btn {
  width: 100%;
  padding: 15px;
  background: #6366f1;
  color: white;
  border: none;
  border-radius: 8px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s;
  margin-bottom: 20px;
  position: relative;
  overflow: hidden;
}

.reset-password-btn:hover {
  background: #4f46e5;
  transform: translateY(-2px);
  box-shadow: 0 8px 20px rgba(99, 102, 241, 0.4);
}

.reset-password-btn:active {
  transform: translateY(0);
}

.reset-password-btn::after {
  content: '';
  position: absolute;
  top: -50%;
  left: -50%;
  width: 200%;
  height: 200%;
  background: radial-gradient(circle, rgba(255,255,255,0.3) 0%, transparent 70%);
  opacity: 0;
  transition: opacity 0.5s;
}

.reset-password-btn:hover::after {
  opacity: 1;
}

.reset-password-btn:disabled {
  opacity: 0.8;
  cursor: not-allowed;
  transform: none;
  box-shadow: none;
}

.error-message {
  color: #ef4444;
  font-size: 13px;
  margin-top: 5px;
}

.back-section {
  text-align: center;
  margin-top: 10px;
}

.back-btn {
  background: transparent;
  color: #94a3b8;
  border: none;
  padding: 8px 16px;
  font-size: 14px;
  cursor: pointer;
  border-radius: 6px;
  transition: all 0.3s;
}

.back-btn:hover {
  background: rgba(148, 163, 184, 0.1);
  color: #e2e8f0;
}

.back-btn i {
  margin-right: 5px;
}

/* 响应式设计 */
@media (max-width: 480px) {
  .forgot-password-container {
    padding: 30px 20px;
    width: 90%;
  }
}
</style>